package com.sumainfor.pc.gmio.service.impl.Excel;

import cn.afterturn.easypoi.entity.vo.MapExcelConstants;
import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.alibaba.fastjson.JSON;
import com.sumainfor.common.easypoiExcel.utils.ExcelStyleUtil;
import com.sumainfor.common.util.Arith;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.utlis.CompuUtils;
import com.sumainfor.common.utlis.ConvertDateTime;
import com.sumainfor.common.utlis.ListMapMergeUtil;
import com.sumainfor.pc.gmio.dao.Excel.ExcelBillMapper;
import com.sumainfor.pc.gmio.entity.ReceiveOwnerRent;
import com.sumainfor.pc.gmio.entity.ReceiveRent;
import com.sumainfor.pc.gmio.feign.ZuulFeign.ZuulFeign;
import com.sumainfor.pc.gmio.feign.extend.HouseFeign;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.MapUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 功能描述:
 * Excel导出账单
 * @Auther: zhlu
 * @Date: 2019/6/17 19:00
 * @Description:
 */
@Service
@Log4j2
public class ExcelBillService {

    @Autowired
    ExcelBillMapper ExcelBillMapper;

    @Autowired
    HouseFeign HouseFeign;

    @Autowired
    ZuulFeign ZuulFeign;

    /**
     * 功能描述:
     * 租金收入列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/17 19:02
     */
    public List<Map<String,Object>> getExcelReceiveRent(Map<String,Object>params){
        //构建房源主表和房源详表数据
        params.putAll(getMiAndComAndBuiAndRoomAndHouList(params));
        log.info("params-------"+params);
        List<Map<String,Object>>getExcelReceiveRent=ExcelBillMapper.getExcelReceiveRent(params);
        if(getExcelReceiveRent.size()>0){
           //获取房源资料
           List<Map<String,Object>>AllHousingLists=HouseFeign.getAllHousingLists();
           //合并房源资料
           getExcelReceiveRent= ListMapMergeUtil.ListMergeUtils(getExcelReceiveRent,AllHousingLists,"HouInfoId");

           //所有服务中心
           List<Map<String,Object>>DeptList=ZuulFeign.getPostDeptList(params);
           getExcelReceiveRent= ListMapMergeUtil.ListMergeUtils(getExcelReceiveRent,DeptList,"DeptId");

           //获取所有的公司列表
           List<Map<String,Object>>getAllAreaList=ZuulFeign.getAllAreaList();
           getExcelReceiveRent= ListMapMergeUtil.ListMergeUtils(getExcelReceiveRent,getAllAreaList,"areaId");

           //获取所有的合同列表
           List<Map<String,Object>>getAllPactList=HouseFeign.getAllPactList();
           getExcelReceiveRent= ListMapMergeUtil.ListMergeUtils(getExcelReceiveRent,getAllPactList,"Source");

           //
           for(Map<String,Object> map:getExcelReceiveRent){
               //支付方式
               //（0月付 1 季付 2 半年付 3年付）
               int Pmethod= MapUtils.getIntValue(map,"Pmethod",0);
               //房屋押金几个月
               int Deposit=MapUtils.getIntValue(map,"Deposit",0);
               //活动后租金   单位：月 （月租金参与活动后的价格）
               double PaRearMonthPrice =MapUtils.getDoubleValue(map,"PaRearMonthPrice",0);

               //押金
               double actualDepositPrice =MapUtils.getDoubleValue(map,"actualDeposit",0);

                if(Pmethod==0){
                    map.put("Pmethod", 1);
                    map.put("rentPrice", CompuUtils.mul(PaRearMonthPrice, 1));
                }else if(Pmethod==1){
                    map.put("Pmethod", 3);
                    map.put("rentPrice", CompuUtils.mul(PaRearMonthPrice, 3));
                }else if(Pmethod==2){
                    map.put("Pmethod", 6);
                    map.put("rentPrice", CompuUtils.mul(PaRearMonthPrice, 6));
                }else if(Pmethod==3){
                    map.put("Pmethod", 12);
                    //租金收入
                    map.put("rentPrice", CompuUtils.mul(PaRearMonthPrice, 12));
                }
                //押金
               map.put("depositPrice", CompuUtils.mul(actualDepositPrice,Deposit));
               //服务中心
               map.put("deptName",map.get("DeptName"));
               //小区名称
               map.put("comName",map.get("ComName"));
               //楼号
               map.put("builDing",map.get("BuilDing"));
               //房号
               map.put("room",map.get("Room"));

               //zc修改 付款方式
               map.put("payment","付"+MapUtils.getInteger(map,"Pmethod")+"押"+Deposit);


             //  //付款方式
              //  map.put("payment","付"+Pmethod+"押"+Deposit);
               //房租应收月租金
               map.put("rearMonthPrice",PaRearMonthPrice);
               //支付形式   0.线上   1.线下
               int Shape=Integer.valueOf(map.get("Shape").toString());
               //支付方式  1.微信  2.支付宝
               int PayMode=Integer.valueOf(map.get("PayMode").toString());
               if(Shape==0){
                    if(PayMode==1){
                        map.put("receipt","微信");
                    }else if(PayMode==2){
                        map.put("receipt","支付宝");
                    }
               }else if(Shape==1){
                   if(PayMode==1){
                       map.put("receipt","微信转账");
                   }else if(PayMode==2){
                       map.put("receipt","支付宝转账");
                   }
               }
           }

        }
        return getExcelReceiveRent;
    }

    /**
     * 功能描述:
     * 租金收入总条数
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/17 19:03
     */
    public Integer getExcelReceiveRentCount(Map<String,Object>params){
        int getExcelReceiveRentCount=0;
        //构建房源主表和房源详表数据
        params.putAll(getMiAndComAndBuiAndRoomAndHouList(params));
        List<String> HouList=(List<String>)params.get("HouList");
        List<String> HouInforList=(List<String>)params.get("HouInforList");
        if(HouList.size()>0||HouInforList.size()>0){
            getExcelReceiveRentCount=ExcelBillMapper.getExcelReceiveRentCount(params);
        }
        return getExcelReceiveRentCount;
    }

    /**
     * 功能描述:
     * 导出收房租金。构建第一个sheet
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/18 10:30
     */
    public Map<String,Object>ExcelReceiveRent(Map<String,Object>params){
        Map<String,Object>result=new HashMap<String,Object>();
        String ExcelTile="收房租金%s~%s统计表";
        //第一个sheet。sheet名和title名
        ExportParams ep=new ExportParams(String.format(ExcelTile,params.get("StartTime"),params.get("EndTime")),"收房租金日期周期统计表");
        //添加默认的样式
        ep.setStyle(ExcelStyleUtil.class);
        result.put(NormalExcelConstants.PARAMS,ep);
        result.put(NormalExcelConstants.CLASS, ReceiveRent.class);
        List<ReceiveRent>list=new ArrayList<ReceiveRent>();
        //构建sheet填充数据
        ReceiveRent receiveRent;
        //获取收房租列表
        List<Map<String,Object>>getExcelReceiveRent=getExcelReceiveRent(params);
        if(getExcelReceiveRent.size()>0){
            //填充数据
            for(Map<String,Object> map:getExcelReceiveRent){
                //把map转实体类
                receiveRent= JSON.parseObject(JSON.toJSONString(map),ReceiveRent.class);
                receiveRent.setDepositPrice1(Double.valueOf(map.get("depositPrice").toString()));
                if(Integer.valueOf(map.get("FirstMons").toString())==1){
                    receiveRent.setDepositPrice2(Double.valueOf(map.get("depositPrice").toString()));
                }else{
                    receiveRent.setDepositPrice2(0);
                }

                list.add(receiveRent);
                log.info("receiveRent------"+receiveRent);
            }
            result.put(NormalExcelConstants.DATA_LIST,list);
        }
        result.put("excelType", 0);//0实体类   1map
        return result;
    }


    /**
     * 功能描述:
     * 组装第二个sheet数据
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/19 14:06
     */
    public Map<String,Object>getExcelReceiveRentInfo(Map<String,Object>params)throws Exception{
        Map<String,Object>result=new HashMap<String,Object>();
        //构建房源主表和房源详表数据
        params.putAll(getMiAndComAndBuiAndRoomAndHouList(params));
        List<String> HouList=(List<String>)params.get("HouList");
        List<String> HouInforList=(List<String>)params.get("HouInforList");
        List<Map<String,Object>>getExcelReceiveRentInfo=new ArrayList<Map<String,Object>>();
        if(HouList.size()>0||HouInforList.size()>0){
            getExcelReceiveRentInfo=ExcelBillMapper.getExcelReceiveRentInfo(params);
        }
        log.info("params-------"+params);
//        List<Map<String,Object>>getExcelReceiveRentInfo=ExcelBillMapper.getExcelReceiveRentInfo(params);
        List<Map<String,Object>>dateTime=new ArrayList<Map<String,Object>>();

        //月份开始时间
        DateTime monthStartTime;
        //月份结束时间
        DateTime monthEndTime;
        //如果开始月份为空，默认当年1月
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"StartTime"})){
            monthStartTime= ConvertDateTime.dateFormatStrToTime("yyyy-MM-dd",String.valueOf(ConvertDateTime.getCurrentDateElement(1))+"-01");
        }else{
            monthStartTime=ConvertDateTime.dateFormatStrToTime("yyyy-MM-dd",params.get("StartTime").toString());
        }
        //如果结束月份为空，默认当年12月
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"EndTime"})){
            monthEndTime= ConvertDateTime.dateFormatStrToTime("yyyy-MM-dd",String.valueOf(ConvertDateTime.getCurrentDateElement(1))+"-12");
        }else{
            monthEndTime=ConvertDateTime.dateFormatStrToTime("yyyy-MM-dd",params.get("EndTime").toString());
        }

        //如果计算开始月份和结束月份有多少个间隔
        //作为循环次数
        int index=ConvertDateTime.subDate(monthStartTime,monthEndTime,2);

        if(getExcelReceiveRentInfo.size()>0){
            //获取房源资料
            List<Map<String,Object>>AllHousingLists=HouseFeign.getAllHousingLists();
            //合并房源资料
            getExcelReceiveRentInfo= ListMapMergeUtil.ListMergeUtils(getExcelReceiveRentInfo,AllHousingLists,"HouInfoId");

            //所有服务中心
            List<Map<String,Object>>DeptList=ZuulFeign.getPostDeptList(params);
            getExcelReceiveRentInfo= ListMapMergeUtil.ListMergeUtils(getExcelReceiveRentInfo,DeptList,"DeptId");

            //获取所有的公司列表
            List<Map<String,Object>>getAllAreaList=ZuulFeign.getAllAreaList();
            getExcelReceiveRentInfo= ListMapMergeUtil.ListMergeUtils(getExcelReceiveRentInfo,getAllAreaList,"areaId");

            //获取所有的合同列表
            List<Map<String,Object>>getAllPactList=HouseFeign.getAllPactList();
            getExcelReceiveRentInfo= ListMapMergeUtil.ListMergeUtils(getExcelReceiveRentInfo,getAllPactList,"Source");

            //构建Excel表格
            Map<String,Object>monthMap;
            for(int i=0;i<=(index+1);i++){
                monthMap=new HashMap<String,Object>();
                //账单生成日期加i
                DateTime StartTime=ConvertDateTime.addDateTime(monthStartTime,i,5);
                //获取开始月份然后加上当前循环月份
                String monthName=ConvertDateTime.dateFormatTimeToStr("yy年MM月",StartTime);
                //Excel表头名称
                monthMap.put("monthName",monthName);
                //字段名
                monthMap.put("monthKey","month"+i);
                //所在年份
                monthMap.put("Year",StartTime.getYear());
                //所在月份
                monthMap.put("Month",StartTime.getMonthOfYear());
                dateTime.add(monthMap);
            }

            for(Map<String,Object> map:getExcelReceiveRentInfo){
                //支付方式
                //（0月付 1 季付 2 半年付 3年付）
                String Pmethod=map.get("Pmethod").toString();
                //房屋押金几个月
                String Deposit=map.get("Deposit").toString();
                //活动后租金   单位：月 （月租金参与活动后的价格）
                double PaRearMonthPrice = Double.valueOf(map.get("PaRearMonthPrice").toString());
                if(Pmethod.equals("0")||Pmethod=="0"){
                    map.put("Pmethod", 1);
                    map.put("rentPrice", CompuUtils.mul(PaRearMonthPrice, 1));
                }else if(Pmethod.equals("1")||Pmethod=="1"){
                    map.put("Pmethod", 3);
                    map.put("rentPrice", CompuUtils.mul(PaRearMonthPrice, 3));
                }else if(Pmethod.equals("2")||Pmethod=="2"){
                    map.put("Pmethod", 6);
                    map.put("rentPrice", CompuUtils.mul(PaRearMonthPrice, 6));
                }else if(Pmethod.equals("3")||Pmethod=="3"){
                    map.put("Pmethod", 12);
                    //租金收入
                    map.put("rentPrice", CompuUtils.mul(PaRearMonthPrice, 12));
                }
                //押金
                map.put("depositPrice", CompuUtils.mul(PaRearMonthPrice, Integer.valueOf(Deposit)));
                //服务中心
                map.put("deptName",map.get("DeptName"));
                //小区名称
                map.put("comName",map.get("ComName"));
                //楼号
                map.put("builDing",map.get("BuilDing"));
                //房号
                map.put("room",map.get("Room"));
                //优惠券
                if(!FieldCheckUtil.isMapFieldNotNull(map,new String[]{"couponamt"})){
                    map.put("couponamt",0);
                }else{
                    map.put("couponamt",Double.valueOf(map.get("couponamt").toString()));
                }
                //付款方式
                map.put("payment","付"+map.get("Pmethod")+"押"+map.get("Deposit"));

                //支付形式   0.线上   1.线下
                int Shape=Integer.valueOf(map.get("Shape").toString());
                //支付方式  1.微信  2.支付宝
                int PayMode=Integer.valueOf(map.get("PayMode").toString());
                if(Shape==0){
                    if(PayMode==1){
                        map.put("receipt","微信");
                    }else if(PayMode==2){
                        map.put("receipt","支付宝");
                    }
                }else if(Shape==1){
                    if(PayMode==1){
                        map.put("receipt","微信转账");
                    }else if(PayMode==2){
                        map.put("receipt","支付宝转账");
                    }
                }
                //房屋应收押金
                map.put("depositPrice1", Double.valueOf(map.get("depositPrice").toString()));
                //房租账单是否是首月    0否   1.是  ，首页才有押金
                if(Integer.valueOf(map.get("FirstMons").toString())==1){
                    //房屋应收押金
                    map.put("depositPrice2", Double.valueOf(map.get("depositPrice").toString()));
                }else{
                    //房屋应收押金
                    map.put("depositPrice2", 0);
                }
                //账单生成日（开始时间）
                DateTime BiGenerated=ConvertDateTime.dateFormatStrToTime("yyyy-MM-dd",map.get("BiGenerated").toString());
                //账单结束时间
                DateTime EndTime=ConvertDateTime.dateFormatStrToTime("yyyy-MM-dd",map.get("EndTime").toString());

                //当前账单总金额
                Double rentPrice=Double.valueOf(map.get("rentPrice").toString());
                //付几
                int method=Integer.valueOf(map.get("Pmethod").toString());
                //分摊金额
                Double price=Arith.div(rentPrice,Double.valueOf(method));
                //房租应收月租金
                map.put("rearMonthPrice",Double.valueOf(map.get("PaRearMonthPrice").toString()));
                log.info("------------------method:"+method);
                for(Map<String,Object> map1:dateTime){
                    //然后根据付几，循环几次，比对属于那一个月份的金额
                    for(int i=0;i<method;i++){
                        //账单生成日期加1
                        DateTime nextDateTime=ConvertDateTime.addDateTime(BiGenerated,i,5);
                        //账单结束时间 和 账单生成日期加1  比较。不能超过账单结束时间
                        int than=ConvertDateTime.compare_date(ConvertDateTime.dateFormatTimeToStr("yyyy-MM-dd",EndTime),ConvertDateTime.dateFormatTimeToStr("yyyy-MM-dd",nextDateTime));
                        log.info("当前比较账单:"+map.get("BiId"));
                        log.info("------------------than"+than);
                        //账单结束时间 和 账单生成日期加1  比较。不能超过账单结束时间
                        if(than>=0){
                            log.info("------------------map1:"+Integer.valueOf(map1.get("Year").toString()));
                            log.info("------------------map1:"+Integer.valueOf(map1.get("Month").toString()));
                            log.info("------------------nextDateTime:"+nextDateTime.getYear());
                            log.info("------------------nextDateTime:"+nextDateTime.getMonthOfYear());
                            if(nextDateTime.getYear()==Integer.valueOf(map1.get("Year").toString())&&
                                    nextDateTime.getMonthOfYear()==Integer.valueOf(map1.get("Month").toString())){
                                log.info("在当前月份");
                                map.put(map1.get("monthKey").toString(),price);
                                break;
                            }else{
                                log.info("不在当前月份");
                                map.put(map1.get("monthKey").toString(),0);
                            }
                        }else{
                            log.info("账单生成日期加1大于了账单结束时间 ");
                            map.put(map1.get("monthKey").toString(),0);
                        }
                    }
                }
            }
        }

        //数据
        result.put("dateList",getExcelReceiveRentInfo);
        //动态月份
        result.put("dateTime",dateTime);
        return result;
    }

    /**
     * 功能描述:
     * 导出收房租金。构建第二个sheet
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/18 10:30
     */
    public Map<String,Object>ExcelReceiveRentInfo(Map<String,Object>params)throws Exception{
        Map<String,Object>result=new HashMap<String,Object>();

        Map<String,Object>ReceiveRentInfo=getExcelReceiveRentInfo(params);
        String ExcelTile="收房租金%s~%s统计表";
        //第一个sheet。sheet名和title名
        ExportParams ep=new ExportParams(String.format(ExcelTile,params.get("StartTime"),params.get("EndTime")),"收房租金月份周期统计表");
        //添加默认的样式
        ep.setStyle(ExcelStyleUtil.class);
        result.put(NormalExcelConstants.PARAMS,ep);

        //组装Excel头
        List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
        ExcelExportEntity name = new ExcelExportEntity("公司", "name");
        name.setWidth(30);
        entity.add(name);
        ExcelExportEntity deptName = new ExcelExportEntity("服务中心", "deptName");
        deptName.setWidth(30);
        entity.add(deptName);
        ExcelExportEntity comName = new ExcelExportEntity("小区名称", "comName");
        comName.setWidth(35);
        entity.add(comName);
        entity.add(new ExcelExportEntity("楼号", "builDing"));
        entity.add(new ExcelExportEntity("房号", "room"));
        entity.add(new ExcelExportEntity("室号", "cod"));
        ExcelExportEntity firstMon = new ExcelExportEntity("新签(续费)", "firstMon");
        firstMon.setWidth(15);
        entity.add(firstMon);
        ExcelExportEntity payment = new ExcelExportEntity("付款方式", "payment");
        payment.setWidth(15);
        entity.add(payment);
        ExcelExportEntity rearMonthPrice = new ExcelExportEntity("房屋应收月租金", "rearMonthPrice");
        rearMonthPrice.setStatistics(true);
        rearMonthPrice.setWidth(20);
        entity.add(rearMonthPrice);
        ExcelExportEntity couponamt = new ExcelExportEntity("优惠券抵扣", "couponamt");
        couponamt.setStatistics(true);
        couponamt.setWidth(20);
        entity.add(couponamt);
        ExcelExportEntity depositPrice1 = new ExcelExportEntity("房屋应收押金", "depositPrice1");
        depositPrice1.setStatistics(true);
        depositPrice1.setWidth(20);
        entity.add(depositPrice1);
        ExcelExportEntity depositPrice2 = new ExcelExportEntity("押金收入", "depositPrice2");
        depositPrice2.setStatistics(true);
        depositPrice2.setWidth(15);
        entity.add(depositPrice2);

        //月份表格数据
        List<Map<String,Object>>dateTime=(List<Map<String,Object>>)ReceiveRentInfo.get("dateTime");
        if(dateTime.size()>0){
            //创建表头
            for(Map<String, Object> map:dateTime){
                ExcelExportEntity exportEntity = new ExcelExportEntity(map.get("monthName").toString(), map.get("monthKey").toString());
                exportEntity.setStatistics(true);
                entity.add(exportEntity);
            }
        }
        ExcelExportEntity rentPrice = new ExcelExportEntity("租金收入合计", "rentPrice");
        rentPrice.setStatistics(true);
        rentPrice.setWidth(20);
        entity.add(rentPrice);
        ExcelExportEntity payMoney = new ExcelExportEntity("收款合计(实付)", "payMoney");
        payMoney.setStatistics(true);
        payMoney.setWidth(20);
        entity.add(payMoney);

        result.put(MapExcelConstants.ENTITY_LIST,entity);

        //合并真实数据
        result.put(NormalExcelConstants.DATA_LIST,(List<Map<String,Object>>)ReceiveRentInfo.get("dateList"));

        result.put("excelType", 1);//0实体类   1map
        return result;
    }


    /**
     * 功能描述:
     * 根据服务中心编号，项目编号。楼号。房号。室号获取房源主表和房源详表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/21 11:56
     */
    public Map<String,Object>getMiAndComAndBuiAndRoomAndHouList(Map<String,Object>params){
        Map<String,Object>result=new HashMap<String,Object>();
        //根据服务中心编号，项目编号。楼号。房号。室号获取房源主表和房源详表
        List<Map<String,Object>>MiAndComAndBuiAndRoomAndHouList=HouseFeign.getMiAndComAndBuiAndRoomAndHouList(params);
        //如果大于0
        if(MiAndComAndBuiAndRoomAndHouList.size()>0){
            List<String> HouList=new ArrayList<String>();
            Set<String> HouSet = new HashSet<String>();
            //合并房源主表
            for(Map<String,Object> map1:MiAndComAndBuiAndRoomAndHouList){
                String keys = map1.get("HouId").toString();
                int beforeSize = HouSet.size();
                HouSet.add(keys);
                int afterSize = HouSet.size();
                if(afterSize == beforeSize + 1){
                    HouList.add(map1.get("HouId").toString());
                }
            }

            List<String> HouInforList=new ArrayList<String>();
            Set<String> HouInforSet = new HashSet<String>();
            //合并房源祥表
            for(Map<String,Object> map2:MiAndComAndBuiAndRoomAndHouList){
                String keys = map2.get("HouInfoId").toString();
                int beforeSize = HouInforSet.size();
                HouInforSet.add(keys);
                int afterSize = HouInforSet.size();
                if(afterSize == beforeSize + 1){
                    HouInforList.add(map2.get("HouInfoId").toString());
                }
            }
            result.put("HouList",HouList);
            result.put("HouInforList",HouInforList);
        }else{
            result.put("HouList",new ArrayList<String>());
            result.put("HouInforList",new ArrayList<String>());
        }
        return result;
    }

    /**
     * 功能描述:
     * 房租支付统计表列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/21 15:34
     */
    public List<Map<String,Object>>getExcelReceiveOwnerRent(Map<String,Object>params){
        //构建房源主表和房源详表数据
        params.putAll(getMiAndComAndBuiAndRoomAndHouList(params));
        List<Map<String,Object>>getExcelReceiveOwnerRent=ExcelBillMapper.getExcelReceiveOwnerRent(params);
        if(getExcelReceiveOwnerRent.size()>0){
            //获取所有的公司列表
            List<Map<String,Object>>getAllAreaList=ZuulFeign.getAllAreaList();
            getExcelReceiveOwnerRent= ListMapMergeUtil.ListMergeUtils(getExcelReceiveOwnerRent,getAllAreaList,"areaId");

            //获取房源资料
            List<Map<String,Object>>AllHousingLists=HouseFeign.getAllHousingLists();
            //合并房源资料
            getExcelReceiveOwnerRent= ListMapMergeUtil.ListMergeUtils(getExcelReceiveOwnerRent,AllHousingLists,"HouId");

            //所有服务中心
            List<Map<String,Object>>DeptList=ZuulFeign.getPostDeptList(params);
            getExcelReceiveOwnerRent= ListMapMergeUtil.ListMergeUtils(getExcelReceiveOwnerRent,DeptList,"DeptId");

            //获取所有的业主列表
            List<Map<String,Object>>getOwnerDealList=HouseFeign.getOwnerDealList();
            getExcelReceiveOwnerRent= ListMapMergeUtil.ListMergeUtils(getExcelReceiveOwnerRent,getOwnerDealList,"OwDeId");


            for(Map<String,Object> map:getExcelReceiveOwnerRent){
                //服务中心
                map.put("deptName",map.get("DeptName"));
                map.remove("DeptName");
                //小区名称
                map.put("comName",map.get("ComName"));
                map.remove("ComName");
                //楼号
                map.put("builDing",map.get("BuilDing"));
                map.remove("BuilDing");
                //房号
                map.put("room",map.get("Room"));
                map.remove("Room");
                //付几
                int Charge=Integer.valueOf(map.get("Charge").toString());
                //押几
                int Pair=Integer.valueOf(map.get("Pair").toString());
                //付款日
                map.put("secondPayment",map.get("secondPayment"));
                //付款方式
                map.put("payment","付"+Charge+"押"+Pair);

                //原始月租
                Double MonthPrice=Double.valueOf(map.get("MonthPrice").toString());
                //增涨月租
                Double MonthPrices=Double.valueOf(map.get("MonthPrices").toString());

                //房租应付月租金
                map.put("rearMonthPrice",MonthPrices);
                //第一期账单才有押金
                //租金支出合计
                Double depositPrice1=0.0;
                if(map.get("Period").toString().equals("1")||map.get("Period").toString()=="1"){
                    if(Pair>1){
                        depositPrice1=Arith.mul(MonthPrices,Double.valueOf(Pair));
                    }else{
                        depositPrice1=MonthPrices;
                    }
                }
                //房屋应付押金
                map.put("depositPrice1",depositPrice1);
                //押金支出
                map.put("depositPrice2",depositPrice1);
                //收款合计
                Double biMoney=Double.valueOf(map.get("biMoney").toString());
                //租金支出合计
                Double expenditure=0.0;
                if(Charge>1){
                    expenditure=Arith.mul(MonthPrices,Double.valueOf(Charge));
                }
                //租金支出合计
                map.put("expenditure",expenditure);
            }
        }
        return getExcelReceiveOwnerRent;
    }

    /**
     * 功能描述:
     * 房租支付统计表列表总数
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/21 16:23
     */
    public Integer getExcelReceiveOwnerRentCount(Map<String,Object>params){
        int getExcelReceiveOwnerRentCount=0;
        //构建房源主表和房源详表数据
        params.putAll(getMiAndComAndBuiAndRoomAndHouList(params));
        List<String> HouList=(List<String>)params.get("HouList");
        List<String> HouInforList=(List<String>)params.get("HouInforList");
        if(HouList.size()>0||HouInforList.size()>0){
            getExcelReceiveOwnerRentCount=ExcelBillMapper.getExcelReceiveOwnerRentCount(params);
        }
        return getExcelReceiveOwnerRentCount;
    }


    /**
     * 功能描述:
     * 导出房租支付统计表。构建第一个sheet
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/18 10:30
     */
    public Map<String,Object>ExcelReceiveOwnerRent(Map<String,Object>params)throws Exception{
        Map<String,Object>result=new HashMap<String,Object>();
        String ExcelTile="房租支付%s-%s统计表";
        //第一个sheet。sheet名和title名
        ExportParams ep=new ExportParams(String.format(ExcelTile,params.get("StartTime"),params.get("EndTime")),"房租支付日期周期统计表");
        //添加默认的样式
        ep.setStyle(ExcelStyleUtil.class);
        result.put(NormalExcelConstants.PARAMS,ep);
        result.put(NormalExcelConstants.CLASS, ReceiveOwnerRent.class);
        List<ReceiveOwnerRent>list=new ArrayList<ReceiveOwnerRent>();
//        List<ReceiveOwnerRent>getExcelReceiveOwnerRent=getExcelReceiveOwnerRent(params);
        //构建sheet填充数据
        ReceiveOwnerRent receiveOwnerRent;
        //获取收房租列表
        List<Map<String,Object>>getExcelReceiveOwnerRent=getExcelReceiveOwnerRent(params);
        if(getExcelReceiveOwnerRent.size()>0){
            //填充数据
            for(Map<String,Object> map:getExcelReceiveOwnerRent){
                //把map转实体类
                receiveOwnerRent= JSON.parseObject(JSON.toJSONString(map),ReceiveOwnerRent.class);
                list.add(receiveOwnerRent);
                log.info("receiveOwnerRent------"+receiveOwnerRent);
            }
            result.put(NormalExcelConstants.DATA_LIST,list);
        }
        result.put("excelType", 0);//0实体类   1map

        return result;
    }

    /**
     * 功能描述:
     * 房租支付统计表详情列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/21 15:34
     */
    public Map<String,Object> getExcelReceiveOwnerRentInfo(Map<String,Object>params)throws Exception{
        Map<String,Object>result=new HashMap<String,Object>();

        //构建房源主表和房源详表数据
        params.putAll(getMiAndComAndBuiAndRoomAndHouList(params));
        List<String> HouList=(List<String>)params.get("HouList");
        List<String> HouInforList=(List<String>)params.get("HouInforList");
        List<Map<String,Object>>getExcelReceiveOwnerRentInfo=new ArrayList<Map<String,Object>>();
        if(HouList.size()>0||HouInforList.size()>0){
            getExcelReceiveOwnerRentInfo=ExcelBillMapper.getExcelReceiveOwnerRentInfo(params);
        }

        //构建Excel动态表头
        List<Map<String,Object>>dateTime=new ArrayList<Map<String,Object>>();
        //月份开始时间
        DateTime monthStartTime;
        //月份结束时间
        DateTime monthEndTime;
        //如果开始月份为空，默认当年1月
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"StartTime"})){
            monthStartTime= ConvertDateTime.dateFormatStrToTime("yyyy-MM-dd",String.valueOf(ConvertDateTime.getCurrentDateElement(1))+"-01");
        }else{
            monthStartTime=ConvertDateTime.dateFormatStrToTime("yyyy-MM-dd",params.get("StartTime").toString());
        }
        //如果结束月份为空，默认当年12月
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"EndTime"})){
            monthEndTime= ConvertDateTime.dateFormatStrToTime("yyyy-MM-dd",String.valueOf(ConvertDateTime.getCurrentDateElement(1))+"-12");
        }else{
            monthEndTime=ConvertDateTime.dateFormatStrToTime("yyyy-MM-dd",params.get("EndTime").toString());
        }

        //如果计算开始月份和结束月份有多少个间隔
        //作为循环次数
        int index=ConvertDateTime.subDate(monthStartTime,monthEndTime,2);

        //构建房源主表和房源详表数据
        if(getExcelReceiveOwnerRentInfo.size()>0){
            //获取所有的公司列表
            List<Map<String,Object>>getAllAreaList=ZuulFeign.getAllAreaList();
            getExcelReceiveOwnerRentInfo= ListMapMergeUtil.ListMergeUtils(getExcelReceiveOwnerRentInfo,getAllAreaList,"areaId");

            //获取房源资料
            List<Map<String,Object>>AllHousingLists=HouseFeign.getAllHousingLists();
            //合并房源资料
            getExcelReceiveOwnerRentInfo= ListMapMergeUtil.ListMergeUtils(getExcelReceiveOwnerRentInfo,AllHousingLists,"HouId");

            //所有服务中心
            List<Map<String,Object>>DeptList=ZuulFeign.getPostDeptList(params);
            getExcelReceiveOwnerRentInfo= ListMapMergeUtil.ListMergeUtils(getExcelReceiveOwnerRentInfo,DeptList,"DeptId");

            //获取所有的业主列表
            List<Map<String,Object>>getOwnerDealList=HouseFeign.getOwnerDealList();
            getExcelReceiveOwnerRentInfo= ListMapMergeUtil.ListMergeUtils(getExcelReceiveOwnerRentInfo,getOwnerDealList,"OwDeId");

            //构建Excel表格
            Map<String,Object>monthMap;
            for(int i=0;i<=(index+1);i++){
                monthMap=new HashMap<String,Object>();
                //账单生成日期加i
                DateTime StartTime=ConvertDateTime.addDateTime(monthStartTime,i,5);
                //获取开始月份然后加上当前循环月份
                String monthName=ConvertDateTime.dateFormatTimeToStr("yy年MM月",StartTime);
                //Excel表头名称
                monthMap.put("monthName",monthName);
                //字段名
                monthMap.put("monthKey","month"+i);
                //所在年份
                monthMap.put("Year",StartTime.getYear());
                //所在月份
                monthMap.put("Month",StartTime.getMonthOfYear());
                dateTime.add(monthMap);
            }


            for(Map<String,Object> map:getExcelReceiveOwnerRentInfo){
                //服务中心
                map.put("deptName",map.get("DeptName"));
                map.remove("DeptName");
                //小区名称
                map.put("comName",map.get("ComName"));
                map.remove("ComName");
                //楼号
                map.put("builDing",map.get("BuilDing"));
                map.remove("BuilDing");
                //房号
                map.put("room",map.get("Room"));
                map.remove("Room");
                //付几
                int Charge=Integer.valueOf(map.get("Charge").toString());
                //押几
                int Pair=Integer.valueOf(map.get("Pair").toString());
                //付款日
                map.put("secondPayment",map.get("secondPayment"));
                //付款方式
                map.put("payment","付"+Charge+"押"+Pair);

                //原始月租
                Double MonthPrice=Double.valueOf(map.get("MonthPrice").toString());
                //增涨月租
                Double MonthPrices=Double.valueOf(map.get("MonthPrices").toString());

                //房租应付月租金
                map.put("rearMonthPrice",MonthPrices);
                //第一期账单才有押金
                //租金支出合计
                Double depositPrice1=0.0;
                if(map.get("Period").toString().equals("1")||map.get("Period").toString()=="1"){
                    if(Pair>1){
                        depositPrice1=Arith.mul(MonthPrices,Double.valueOf(Pair));
                    }else{
                        depositPrice1=MonthPrices;
                    }
                }
                //房屋应付押金
                map.put("depositPrice1",depositPrice1);
                //押金支出
                map.put("depositPrice2",depositPrice1);
                //收款合计
                Double biMoney=Double.valueOf(map.get("biMoney").toString());
                //租金支出合计
                Double expenditure=0.0;
                if(Charge>1){
                    expenditure=Arith.mul(MonthPrices,Double.valueOf(Charge));
                }
                //租金支出合计
                map.put("expenditure",expenditure);


                //账单生成日（开始时间）
                DateTime startTime=ConvertDateTime.dateFormatStrToTime("yyyy-MM-dd",map.get("startTime").toString());
                //账单结束时间
                DateTime endTime=ConvertDateTime.dateFormatStrToTime("yyyy-MM-dd",map.get("endTime").toString());

                //分摊金额
                Double price=Arith.div(biMoney,Double.valueOf(Charge));

                for(Map<String,Object> map1:dateTime){
                    //然后根据付几，循环几次，比对属于那一个月份的金额
                    for(int i=0;i<Charge;i++){
                        //账单生成日期加1
                        DateTime nextDateTime=ConvertDateTime.addDateTime(startTime,i,5);
                        //账单结束时间 和 账单生成日期加1  比较。不能超过账单结束时间
                        int than=ConvertDateTime.compare_date(ConvertDateTime.dateFormatTimeToStr("yyyy-MM-dd",endTime),ConvertDateTime.dateFormatTimeToStr("yyyy-MM-dd",nextDateTime));
                        log.info("当前比较账单:"+map.get("BiOutId"));
                        log.info("------------------than"+than);
                        //账单结束时间 和 账单生成日期加1  比较。不能超过账单结束时间
                        if(than>=0){
                            log.info("------------------map1:"+Integer.valueOf(map1.get("Year").toString()));
                            log.info("------------------map1:"+Integer.valueOf(map1.get("Month").toString()));
                            log.info("------------------nextDateTime:"+nextDateTime.getYear());
                            log.info("------------------nextDateTime:"+nextDateTime.getMonthOfYear());
                            if(nextDateTime.getYear()==Integer.valueOf(map1.get("Year").toString())&&
                                    nextDateTime.getMonthOfYear()==Integer.valueOf(map1.get("Month").toString())){
                                log.info("在当前月份");
                                map.put(map1.get("monthKey").toString(),price);
                                break;
                            }else{
                                log.info("不在当前月份");
                                map.put(map1.get("monthKey").toString(),0);
                            }
                        }else{
                            log.info("账单生成日期加1大于了账单结束时间 ");
                            map.put(map1.get("monthKey").toString(),0);
                        }
                    }
                }
            }
        }
        //数据
        result.put("dateList",getExcelReceiveOwnerRentInfo);
        //动态月份
        result.put("dateTime",dateTime);
        return result;
    }

    /**
     * 功能描述:
     * 导出房租支付统计表。构建第二个sheet
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/18 10:30
     */
    public Map<String,Object>ExcelReceiveOwnerRentInfo(Map<String,Object>params)throws Exception{
        Map<String,Object>result=new HashMap<String,Object>();

        Map<String,Object>getExcelReceiveOwnerRentInfo=getExcelReceiveOwnerRentInfo(params);
        String ExcelTile="房租支付%s-%s统计表";
        //第一个sheet。sheet名和title名
        ExportParams ep=new ExportParams(String.format(ExcelTile,params.get("StartTime"),params.get("EndTime")),"房租支付月份周期统计表");
        //添加默认的样式
        ep.setStyle(ExcelStyleUtil.class);
        result.put(NormalExcelConstants.PARAMS,ep);
        //组装Excel头
        List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
        ExcelExportEntity name = new ExcelExportEntity("所属公司", "name");
        name.setWidth(30);
        entity.add(name);
        ExcelExportEntity deptName = new ExcelExportEntity("所属服务中心", "deptName");
        deptName.setWidth(30);
        entity.add(deptName);
        ExcelExportEntity comName = new ExcelExportEntity("小区名称", "comName");
        comName.setWidth(35);
        entity.add(comName);
        entity.add(new ExcelExportEntity("楼号", "builDing"));
        entity.add(new ExcelExportEntity("房号", "room"));
        ExcelExportEntity secondPayment = new ExcelExportEntity("付款日", "secondPayment");
        secondPayment.setWidth(10);
        entity.add(secondPayment);
        ExcelExportEntity payment = new ExcelExportEntity("付费方式", "payment");
        payment.setWidth(15);
        entity.add(payment);
        ExcelExportEntity rearMonthPrice = new ExcelExportEntity("房租应付月租金", "rearMonthPrice");
        rearMonthPrice.setStatistics(true);
        rearMonthPrice.setWidth(20);
        entity.add(rearMonthPrice);
        ExcelExportEntity depositPrice1 = new ExcelExportEntity("房屋应付押金", "depositPrice1");
        depositPrice1.setStatistics(true);
        depositPrice1.setWidth(20);
        entity.add(depositPrice1);
        ExcelExportEntity depositPrice2 = new ExcelExportEntity("押金支出", "depositPrice2");
        depositPrice2.setStatistics(true);
        depositPrice2.setWidth(15);
        entity.add(depositPrice2);

        //月份表格数据
        List<Map<String,Object>>dateTime=(List<Map<String,Object>>)getExcelReceiveOwnerRentInfo.get("dateTime");
        if(dateTime.size()>0){
            //创建表头
            for(Map<String, Object> map:dateTime){
                ExcelExportEntity exportEntity = new ExcelExportEntity(map.get("monthName").toString(), map.get("monthKey").toString());
                exportEntity.setStatistics(true);
                entity.add(exportEntity);
            }
        }

        ExcelExportEntity expenditure = new ExcelExportEntity("租金支出合计", "expenditure");
        expenditure.setStatistics(true);
        expenditure.setWidth(20);
        entity.add(expenditure);
        ExcelExportEntity biMoney = new ExcelExportEntity("付款合计", "biMoney");
        biMoney.setStatistics(true);
        biMoney.setWidth(20);
        entity.add(biMoney);
        result.put(MapExcelConstants.ENTITY_LIST,entity);
        //合并真实数据
        result.put(NormalExcelConstants.DATA_LIST,(List<Map<String,Object>>)getExcelReceiveOwnerRentInfo.get("dateList"));

        result.put("excelType", 1);//0实体类   1map

        return result;
    }

}
